#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDebug>
#include <QComboBox>
#include "parameter/seatdetection.h"
#include "dialog/defectdetectiondialog.h"
#include "dialog/seatparameterdialog.h"
#include "view/camerainfowidget.h"
#include "network/httpserver.h"
/////////////////////////////////////
#include "description/JakaRobotDevice.h"
#include "description/RS232Device.h"

#define IMAGE_WIDTH 5496
#define IMAGE_HEIGHT 3672
#define USE_CUDA false //use opencv-cuda

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    void            InitLib();
    void            UninitLib();

private slots:
    void on_pushButtonRun_clicked();

    void on_pushButtonTest_clicked();

    void on_pushButtonParameter_clicked();

    void on_pushButtonIndexDesign_clicked();

    void on_pushButtonHelp_clicked();

    void on_pushButtonSaveImage_clicked();

    void on_pushButtonSanpImage_clicked();

    void on_pushButtonRecognizeImage_clicked();

    void on_pushButtonRobotConnect_clicked();

    void on_pushButtonRobotPower_clicked();

    void on_pushButtonRobotEnable_clicked();

    void on_pushButtonRobotMove_clicked();

    void on_pushButtonOpenSerialPort_clicked();

    void on_pushButtonSendData_clicked();

    void on_pushButtonTelescopicLeft_clicked();

    void on_pushButtonTelescopicRight_clicked();

    void on_pushButtonArea_clicked();

public slots:
    void on_finishSanpImage_signal(int index);
    void on_finishRecognizeImage_signal(int index);

    void on_descriptionReceiveMessage(QString message);

signals:
    void updateSubImage(int index);

    void sendReceivedMessage(QString message);

private:
    void        InitSeatFacePostion();
    void        InitComboCameras(QComboBox* pComboBox,int number);
    void        InitSeatFaces(QComboBox* pComboBox,int number);

    Device*     findCameraDeviceByImageName(int imageNum);

public:
    void        RecaputraCameraVideo();
    void        TocaputraCameraImage();

private:
    SeatDetection               m_objSeatDetection;
    DefectDetectionDialog       *m_pDefectDetectionDialog;
    SeatParameterDialog         *m_pSeatParameterDialog;

    JakaRobotDevice*            m_pJakaRobotDevice;
    RS232Device*                m_pRS232Device;

public:
    SeatDetection*              GetSeatDetection(){return &m_objSeatDetection;}

    QString                     m_strParameterPath;

private:
    int                     m_nImageWidth,m_nImageHeight,m_nImageChannel;
    // 所有未处理图像的缓冲区
    char*                   m_pImageSrcBuffer;
    // 所有处理后图像的缓冲区
    char*                   m_pImageDestBuffer;
    // 所有处理后图像的缺陷明细信息
    std::vector<std::vector<Detection>> m_vecDetectionResults;

    void                    AllocImageBuffer();
    void                    FreeImageBuffer();

    void                    InitSeatParameter();
    // 所有子图像显示控件的缓冲区
    CameraViewWidget        **m_ppImageCameraViewWidgets;
    CameraInfoWidget        **m_ppInfoWidgets;

    QVector<QWidget*>       m_vecpVideoWidgets;

    QVector<QComboBox*>     m_vecCameraIndexComboBoxs;
    QVector<QComboBox*>     m_vecFaceIndexComboBoxs;

public:
    char*                   GetImageSrcBuffer(int index);
    char*                   GetImageDestBuffer(int index);
    // 操作图像缺陷的方法
    void                    ClearDetectionResult(){m_vecDetectionResults.clear();}
    void                    PushDetectionResult(std::vector<Detection> detection);
    std::vector<Detection>  GetDetectionResult(int index);

    CameraInfoWidget        **GetInfoWidgets(){return m_ppInfoWidgets;}
    CameraInfoWidget        *GetInfoWidget(int index){return m_ppInfoWidgets[index];}
    CameraViewWidget        **GetImageCameraViewWidgets(){return m_ppImageCameraViewWidgets;}
    CameraViewWidget        *GetImageCameraViewWidget(int index){return m_ppImageCameraViewWidgets[index];}

    void                    InitMainImageViewWidget();
    void                    InitVideoViewWidget();
    void                    InitImageViewWidget();
    void                    InitImageListWidget();

    void                    InitStatusBar();

    void                    InitHttpServer();

    void                    InitHardwareTest();

    void                    InitComboBoxDatas();

    void                    InitDevices();
    void                    UninitDevices();
    void                    GetJakaRobotStatus();

public:
    static MainWindow*          instance();

private:
    static MainWindow*          _instance;

    GX_DEVICE_INFO          stDeviceInfo;

private:
    HttpServer               m_objHttpServer;

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
